﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.Linq;
using System.Configuration;
using Kronos.Classes;
using Kronos.DatabaseModels;
using System.Drawing;
using System.Security.Cryptography;
using System.Text;
namespace Kronos.Classes
{
    public class LoginModel:BaseUserModel
    {
        protected string pwd;
        public LoginModel(string username, string password, Label notifiy)
            :base(username,notifiy)
        {

            pwd = password;

            if (checkValidInformationEntered() && UserIsLoggedIn())
            {
                string loginURL = "~/Pages/LoggedIn.aspx";
                String redirectURL;

                int queryAdd = loginURL.IndexOf('?');
                if (queryAdd == -1)
                {
                   redirectURL = loginURL + "?Username="+EncryptUrl(UserIn);
                   HttpContext.Current.Response.Redirect(redirectURL, true);
                }
                base.WriteMessageLabel("Login Validated");
                
            }
        }

        public virtual bool checkValidInformationEntered()
        {
            if (UserIn == "" || pwd == "")
            {
                base.WriteMessageLabel("Invalid Data");
                return false;
            }
            else
                return true;

        }

        public  bool UserIsLoggedIn()
        {
            string encryptionpwd = EncryptPwd(pwd);
            var retrieved_user = (from person in soen341dB_context.Users
                                  where person.hashedPassword == encryptionpwd
                                  where person.username == UserIn
                                  select person).FirstOrDefault();

            //check to see if a record of it exists
            if (retrieved_user == null) {base.WriteMessageLabel("Account Information Entered is Incorrect"); return false; }
            else
            {

                base.WriteMessageLabel("Login Validated");

                return true;

            }

        }

        public static string UserRedirectionValidation()
        {
            
            string UserLogged=HttpContext.Current.Request.QueryString["Username"];
            if(UserLogged!=null)
            {
            byte[] decbuff = Convert.FromBase64String(UserLogged);
             UserLogged = System.Text.Encoding.UTF8.GetString(decbuff);
            }

            else{HttpContext.Current.Response.Redirect("~/Default.aspx");}

            if (HttpContext.Current.Request.QueryString["Username"] == null ||
                HttpContext.Current.Request.QueryString["Username"] == "")
            { HttpContext.Current.Response.Redirect("~/Default.aspx"); }


            if (HttpContext.Current.Request.QueryString["Username"] != null) { return UserLogged; }
            else
                return null;

        }




    }
}